﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;

namespace Common.Tools
{
    public class XmlConfigHelper
    {
        const string varRoot = "VarTable";
        XmlDocument doc = new XmlDocument();

        private string fileName = null;

        /// <summary>
        /// 配置文件名
        /// </summary>
        public string FileName
        {
            get { return fileName; }
        }
        private Hashtable varTable = new Hashtable();

        public Hashtable VarTable
        {
            get { return new Hashtable(varTable); }
        }
        public XmlConfigHelper(string filename)
        {
            fileName = filename;
            if (System.IO.File.Exists(fileName))
            {
                doc.Load(fileName);
                XmlNodeList nodeList = doc.SelectSingleNode(varRoot).ChildNodes;
                foreach (XmlNode node in nodeList)
                {
                    XmlElement tmp = (XmlElement)node;
                    varTable[tmp.Name] = tmp.InnerText;
                }
            }
            else
            {
                XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                doc.AppendChild(declaration);
                XmlElement root = doc.CreateElement(varRoot);
                doc.AppendChild((XmlNode)root);
                doc.Save(fileName);
            }
        }


        public string this[string element]
        {
            get { return (string)varTable[element]; }
            set
            {
                varTable[element] = value;
                XmlNode root = doc.SelectSingleNode(varRoot);
                XmlNodeList nodeList = root.ChildNodes;
                foreach (XmlNode node in nodeList)
                {
                    XmlElement tmp = (XmlElement)node;
                    if (tmp.Name == element)
                    {
                        tmp.InnerText = (string)varTable[element];
                        doc.Save(fileName);
                        return;
                    }
                }
                //没有则创建
                XmlElement newElement = doc.CreateElement(element);
                newElement.InnerText = (string)varTable[element];
                root.AppendChild((XmlNode)newElement);
                doc.Save(fileName);
            }
        }



        /// <summary>
        /// 移除配置
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            XmlNode root = doc.SelectSingleNode(varRoot);
            XmlNodeList nodeList = root.ChildNodes;
            foreach (XmlNode node in nodeList)
            {
                if (node.Name == key)
                {
                    varTable.Remove(key);
                    root.RemoveChild(node);
                    break;
                }
            }
            doc.Save(fileName);
        }

        public void RemoveAll()
        {
            doc.SelectSingleNode(varRoot).RemoveAll();
            varTable.Clear();
            doc.Save(fileName);
        }

        public static void Initialize()
        {
            string currentPath = System.Environment.CurrentDirectory;
            XmlConfigHelper tool = new XmlConfigHelper(currentPath + "//Config.xml");
            tool["HTTP_PATH_ROOT"] = "http://iten/moolly";
            tool["PATH_ROOT"] = "X:";
            tool["USERICON_PATH"] = "moolly//user_icon";    //tool["Path_Root"]的子级目录
            tool["GOODS_PATH"] = "moolly//goods_info";      //tool["Path_Root"]的子级目录
            tool["BRANDS_PATH"] = "moolly//brand_info";     //tool["Path_Root"]的子级目录
            tool["USERICON_PATH"] = "moolly//adinfo";      //tool["Path_Root"]的子级目录
        }
    }
}
